package com.als.healthymananger.controller.impl;

import com.als.healthymananger.controller.UsersApi;
import com.als.healthymananger.entity.User;
import com.als.healthymananger.service.UsersService;
import com.als.healthymananger.utils.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("/system/user")
public class UsersController implements UsersApi {
    @Autowired
    private UsersService usersService;

    @Override
    public RespBean createUsers(User user) {
        return usersService.save(user)? RespBean.ok("添加成功"): RespBean.badRequest("添加失败");
    }

    @Override
    public RespBean deleteUsers(String username) {
        return usersService.removeById(username)? RespBean.ok("删除成功"): RespBean.badRequest("删除失败");
    }

    @Override
    public RespBean updateUsers(int username, User user) {
        return usersService.updateById(user)? RespBean.ok("修改成功"): RespBean.badRequest("修改失败");
    }

    @Override
    public RespBean getUsers(String username) {
        User user = usersService.getById(username);
        return user == null? RespBean.badRequest("获取失败"): RespBean.ok("获取成功", user);
    }
}
